package org.naxx.security.util;

import java.io.File;

/**
 * Fail-fast error checking utility methods.
 */
public final class Assert {
	
	public static void assertNotNull(Object target, String message) {
		if(target == null) {
			throw new IllegalArgumentException(message);
		}
	}

	public static final void assertNull(Object object, String message) {
		if(object != null) {
			throw new IllegalStateException(message);
		}
	}
	
	/**
	 * If supplied expression is not true throws exception
	 * with supplied string.
	 */
	public static final void assertTrue(boolean expression, String message) {
		if(!expression) {
			throw new IllegalArgumentException(message);
		}
	}
	
	public static void assertFileExistsAndReadable(File file) {
		if(!file.exists()) {
			throw new IllegalArgumentException("File '" + file.getAbsolutePath() + "'" +
					" does not exist");
		}
		if(!file.canRead()) {
			throw new IllegalArgumentException("File '" + file.getAbsolutePath() + "'" +
					" can not be read");
		}
	}
	
	public static void assertNotNullAndHasLenght(String target, String message) {
		if(target == null || "".equals(target)) {
			throw new IllegalArgumentException(message);
		}
	}
	
	private Assert() {
		throw new IllegalStateException("Can not instantiate class '" 
				+ Assert.class.getCanonicalName() + "'");
	}
}
